// pages/joke/joke.js 聚合
var uitls = require("../../utils/util.js")

Page({

  /**
   * 页面的初始数据
   */
  data: {
    page: 1,
    contentList: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getjokelist()
  },

  bindload: function (event) {
    console.log(event)
  },

  binderror: function (event) {
    console.log(event)
  },

  getjokelist: function () {
    var that = this;
    wx.showLoading({
      title: '正在加载...',
    })
    wx.request({
      data: {
        // showapi_sign: "2ddab1e30ca69a0041f785f64a3792e3",
        appkey: "75b21f402da0f4ae8b0a034af9a6347e",
        page: that.data.page
      },
      header: {
        "content-type": "application/x-www-form-urlencoded"
      },
      url: 'https://v.juhe.cn/joke/content/text.php' + "?key=75b21f402da0f4ae8b0a034af9a6347e" + "&page=" + that.data.page,// 'https://way.jd.com/showapi/wbxh' 
      method: "GET",
      success: function (res) {
        wx.hideLoading()
        wx.stopPullDownRefresh();
        let code = res.data.error_code
        if (code == 0) {
          var contentList = []
          contentList = res.data.result.data
          contentList.reverse()
          if (that.data.page > 1) {
            contentList = that.data.contentList.concat(contentList)
          }

          that.setData({
            contentList: contentList
          })
        }
        else {
          wx.showToast({
            title: res.data.reason,
            icon: 'none'
          })
        }
      },
      fail: function (error) {
        wx.hideLoading()
        wx.stopPullDownRefresh();
        console.log(error)
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.setData({
      page: 1,
      contentList: []
    })
    this.getjokelist()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.data.page++
    this.getjokelist()
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})